<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('添加产品图片')" />
    <th:block th:include="include :: ztree-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-manage-view">
        <div class="form-group">
            <label class="col-sm-3 control-label ">排序值：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="phoGalSort" id="phoGalSort" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">图片地址：</label>
            <input id="filePath" name="filePath" class="form-control" type="file">
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: ztree-js" />
<th:block th:include="include :: jasny-bootstrap-css" />
<th:block th:include="include :: jasny-bootstrap-js" />
<script type="text/javascript">


    $("#form-series-add").validate({
        rules:{
            onkeyup: false,
            seriesName:{
                remote: {
                    url: ctx + "datum/series/checkSeriesNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "seriesName" : function() {
                            return $.common.trim($("#seriesName").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            roleSort:{
                digits:true
            },
        },
        messages: {
            "seriesName": {
                remote: "产品系列名称已经存在"
            },
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            uploadFile();
        }
    }

    function add() {
        var seriesName = $("input[name='seriesName']").val();
        var seriesDesc = $("textarea[name='seriesDesc']").val();
        $.ajax({
            cache : true,
            type : "POST",
            url : ctx + "datum/series/add",
            data : {
                "seriesName": seriesName,
                "seriesDesc": seriesDesc,
            },
            async : false,
            error : function(request) {
                $.modal.alertError("系统错误");
            },
            success : function(data) {
                $.operate.successCallback(data);
            }
        });
    }

    /* 文件上传 */
    function uploadFile() {
        var formData = new FormData();
        if ($('#filePath')[0].files[0] == null) {
            $.modal.alertWarning("请先选择文件路径");
            return false;
        }
        formData.append('filePath', $("#filePath").val());
        formData.append('file', $('#filePath')[0].files[0]);
        $.ajax({
            url: ctx + "/common/upload",
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function(result) {
                fileName = result.fileName;
                url = result.url;
                console.log(fileName + " " + url);
                if(result) {
                    parent.$.modal.msgSuccess(result.msg);
                }
            }
        });
    }
</script>
</body>
</html>
